package com.lzx.loginandregister.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.loginandregister.entity.User;
import com.lzx.loginandregister.mapper.UserMapper;
import com.lzx.loginandregister.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    //登录
    @Override
    public Map<String, Object> login(User user) {
        Map<String, Object> map = new HashMap<>();

        //根据手机号查询用户信息
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", user.getMobile());
        User vo = userMapper.selectOne(wrapper);

        if (vo == null) {
            //状态码
            map.put("code", 400);
            //消息
            map.put("message", "手机号不存在");
        } else {
            if (Objects.equals(user.getPassword(), vo.getPassword())) {
                //状态码
                map.put("code", 200);
                //用户信息
                map.put("user", vo);
            } else {
                //状态码
                map.put("code", 400);
                //用户信息
                map.put("message", "密码错误");
            }
        }
        return map;
    }

    //注册
    @Override
    public String register(User user) {
        user.setCreateTime(new Date());
        userMapper.insert(user);
        return "注册成功";
    }
}
